<?php

/**
 *
 *
 * @version $Id: class_fun.inc.php,v 1.20 2005/01/06 17:54:18 phppc Exp $
 * @copyright 2004
 **/

//$phpversion = phpversion();
//$phpversion = str_replace (".","",$phpversion);
//$phpversion = intval($phpversion);
//if ($phpversion < 410) {
//    $_GET = $HTTP_GET_VARS;
//    $_POST = $HTTP_POST_VARS;
//    $_ENV = $HTTP_ENV_VARS;
//    $_SERVER = $HTTP_SERVER_VARS;
//}

/**
 * functions
 *
 * @package
 * @author Armin
 * @copyright Copyright (c) 2004
 * @version $Id: class_fun.inc.php,v 1.20 2005/01/06 17:54:18 phppc Exp $
 * @access public
 **/
class functions extends phpnc_db{
	var $attr_ids = array(); // array with attribute ids
	var $fun_check_speed_array = array(); // array with all information needed for check speed function
	/**
	 * functions::fun_lim_news()
	 *
	 * @param $news_orig
	 * @param $length
	 * @param integer $type
	 * @param integer $show_points
	 * @return
	 **/
	function fun_lim_news($news_orig,$length,$type = 2,$show_points = 1) {
		$shorter = 0;
		if ($type == 2 OR $type == 3) {
		    $news_first = substr($news_orig,0,$length);
			$letztes_leer = strrpos($news_first," ");
			$news_second = substr($news_orig,$length);
			$erstes_leer_second = strpos($news_second," ");
			if (($letztes_leer != $length - 1 AND $erstes_leer_second != 0)) {
				$news_second = substr($news_second,0,strpos ($news_second, " "));
				$news = $news_first . $news_second;
			}elseif ($erstes_leer_second === false) $news = $news_first . $news_second;
			else $news = $news_first;
			if($type == 3 AND strlen($news) > $length) $news = substr($news_orig,0,$length);
		}elseif ($type == 1) $news = substr($news_orig,0,$length);
		elseif($type == 0) $news = $news_orig;
		if(strlen($news) < strlen($news_orig)) $shorter = 1;
		if((strlen($news) < strlen($news_orig)) AND $show_points == 1) $news .= "&nbsp;...&nbsp;";
		$news = stripslashes($news);
		if($type == 2 OR $type == 3) $preview = $news_first . "<!--Ende Anzeige-->" . $news_second;
		else $preview = $news;
		return array($news,$shorter,$preview);
	}

	/**
	 * functions::fun_getmicrotime()
	 *
	 * @return
	 **/
	function fun_getmicrotime(){
	    list($usec, $sec) = explode(" ",microtime());
	    return ((float)$usec + (float)$sec);
	}

	/**
	 * functions::fun_get_attr_data()
	 *
	 * @param $only_ids
	 * @return
	 **/
	function fun_get_attr_data($only_ids) {
		if(count($only_ids > 0))$where_part = " WHERE " . $this->uf_create_where_part($only_ids,"news_id");
		$erg = $this->db_abfrage_q("SELECT attr_id,news_id FROM ".$this->config["prefix"]."attr_main$where_part");
		$only_attrs = array();
		while(list($attr_id,$news_id) = $this->db_abfrage_fetch_q($erg)){
			if(!is_array($news_attr_ids[$news_id])) $this->attr_ids[$news_id] = array();
			array_push($this->attr_ids[$news_id],$attr_id);
			array_push($only_attrs,$attr_id);
		} // while
		$where_part = "";
		if(count($only_attrs) > 0)$where_part = " WHERE " . $this->uf_create_where_part($only_attrs);
		$erg = $this->db_abfrage_q("SELECT id,name,img FROM ".$this->config["prefix"]."attr$where_part");
		while($attr_db = $this->db_abfrage_fetch($erg)){
			$this->db_attr[$attr_db["id"]] = $attr_db;
		} // while
	}

	/**
	 * functions::fun_email_conf()
	 *
	 * @param $email
	 * @return
	 **/
	function fun_email_conf($email) {
		$valid_email = 0;
		if (preg_match("/[a-z0-9_-]+(\.[a-z0-9_-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4}|museum)/i","$email")) $valid_email = 1;
		return $valid_email;
	}

	/**
	 * functions::fun_random_value()
	 *
	 * @param integer $ende
	 * @return
	 **/
	function fun_random_value($ende = 10) {
		$random_value = "";
		srand((double)microtime()*1000000);
		for ($i=1; $i<=$ende; $i++) {
	    	$random_value .= chr(rand(65,90));
		}
		$random_value = strtolower($random_value);
		return $random_value;
	}

	/**
	 * functions::fun_tstamp2date()
	 *
	 * @param $timestamp
	 * @param string $datums_format
	 * @param string $zeit_format
	 * @return
	 **/
	function fun_tstamp2date($timestamp,$datums_format = "d.m.Y",$zeit_format = "H:i") {
		$timestamp = $timestamp + $this->time_add;
		$datum = gmdate ($datums_format, $timestamp);
		$zeit = gmdate ($zeit_format,$timestamp);
		$werte = array($datum,$zeit,$timestamp);
		return $werte;
	}

	/**
	 * functions::fun_sg2var()
	 *
	 * @param $var_name
	 * @param integer $cookie
	 * @return
	 **/
	function fun_sg2var ($var_name,$cookie = 0) {
		$$var_name = "";
		if (isset($_POST["$var_name"])) $$var_name = $_POST["$var_name"];
		elseif (isset($_GET["$var_name"])) $$var_name = $_GET["$var_name"];
		elseif (isset($_COOKIE["$var_name"]) AND $cookie == 1) $$var_name = $_COOKIE["$var_name"];
		return $$var_name;
	}

	/**
	 * functions::fun_make_link()
	 *
	 * @param $file
	 * @param string $var_string
	 * @param integer $set_ncs
	 * @param array $module_ignore
	 * @return
	 **/
	function fun_make_link ($file,$var_string = "",$set_ncs = 1,$module_ignore = array()) {
		if($file == "") $file = $this->config["http_pfad"].$this->config["standard_file"];
		$link = $file;
		$add_var_string = "";
		foreach ($this->module_extern_array as $intern_name => $extern_name) {
			if(!isset($this->module_new_value[$intern_name])) $this->module_new_value[$intern_name] = "";
			if($this->uf_form_conf_def($intern_name,$module_ignore,"","valid_input") != 1) $add_var_string .= $extern_name . '=' . $this->module_new_value[$intern_name] . '&';
		}
		$var_string = $add_var_string . $var_string;
		if ($set_ncs == 1) {
			$link .= "?".$this->config["ncs_var"]."=".$this->ncs;
		    if($var_string != "") $link .= "&$var_string";

		}else {
			if($var_string != "") $link .= "?" . $var_string;
		}
		return $link;
	}

	/**
	 * functions::fun_nav_page_creator()
	 *
	 * @param $anzahl_gesamt
	 * @param $per_page
	 * @param integer $page
	 * @param integer $amount_buttons
	 * @param $show_whole_pages
	 * @param $zus_var
	 * @param integer $anzahl_seiten
	 * @param string $page_var
	 * @param string $file
	 * @param string $l_temp
	 * @param string $m_temp
	 * @param string $m_temp_dis
	 * @param string $r_temp
	 * @param string $temp
	 * @param string $f_temp
	 * @param string $la_temp
	 * @return
	 **/
	function fun_nav_page_creator($anzahl_gesamt,$per_page,$page = 1,$amount_buttons = 5,$show_whole_pages,$zus_var,$anzahl_seiten = 0,$page_var = "page",$file = "",$l_temp = "news_nav_left",$m_temp = "news_nav_middle",$m_temp_dis = "news_nav_middle_dis",$r_temp = "news_nav_right",$temp = "news_nav",$f_temp = "news_nav_first",$la_temp = "news_nav_last") {
		$nav_first = "";
		$nav_left = "";
		$nav_middle = "";
		$nav_right = "";
		$nav_last = "";
		$news_nav = "";
		if ($anzahl_seiten == 0) {
		    $anzahl_seiten = ceil($anzahl_gesamt / $per_page);
			if ($page > $anzahl_seiten) {
			    $page = $anzahl_seiten;
			}
		}
		$trenner1 = floor($amount_buttons/2);
		$trenner2 = $trenner1;
		$beginn = $page - $trenner1;
		$ende = $page + $trenner2;
		$end = $ende;
		$unterschied1 = $page - $trenner1;
		if($unterschied1 <= 0) $end += abs($unterschied1) + 1;
		if($end > $anzahl_seiten) $end = $anzahl_seiten;
		$unterschied2 = $anzahl_seiten - $page;
		if($unterschied2 < $trenner2) $beginn -= ($trenner2 - $unterschied2);
		if($beginn >= 1) $i = $beginn;
		else $i = 1;
		if($page < 1) $page = 1;

		if ($anzahl_seiten > 1) {
		   	if ($page > 1) {
				$left_page = $page - 1;
				$button_left = $this->fun_make_link($file,"$page_var=$left_page$zus_var");
				$this->tpl_load($l_temp);
				$this->tpl_replace_vars(array("button_left" => $button_left));
				$nav_left = $this->tpl_output();
			}
			if ($page > 2) {
				$button_first = $this->fun_make_link($file,"$page_var=1$zus_var");
				$this->tpl_load($f_temp);
				$this->tpl_replace_vars(array("button_first" => $button_first));
				$nav_first = $this->tpl_output();
			}
			for ($i;$i <= $end;$i++) {
				$button_middle_one = $this->fun_make_link("$file","$page_var=$i$zus_var");
				if($page == $i) $m_tpl = $m_temp_dis;
				else $m_tpl = $m_temp;
				$this->tpl_load($m_tpl);
				$this->tpl_replace_vars(array("button_middle_one" => $button_middle_one,"i" => $i));
				$nav_middle .= $this->tpl_output();
			}
			if ($page < $anzahl_seiten - 1 AND $page >= 1) {
				$button_last = $this->fun_make_link($file,"$page_var=$anzahl_seiten$zus_var");
				$this->tpl_load($la_temp);
				$this->tpl_replace_vars(array("button_last" => $button_last));
				$nav_last = $this->tpl_output();
			}
			if ($page < $anzahl_seiten AND $page >= 1) {
				$next_page = $page + 1;
				$button_right = $this->fun_make_link("$file","$page_var=$next_page$zus_var");
				$this->tpl_load($r_temp);
				$this->tpl_replace_vars(array("button_right" => $button_right));
				$nav_right = $this->tpl_output();
			}
			$this->tpl_load($temp);
			$this->tpl_replace_vars(array("nav_first" => $nav_first,"nav_left" => $nav_left,"nav_middle" => $nav_middle,"nav_right" => $nav_right,"nav_last" => $nav_last));
			$news_nav .= $this->tpl_output();
		}
		if($page == 0) $page = 1;
		$start = $per_page * $page - $per_page;
		return array($start,$news_nav);
	}

	/**
	 * functions::fun_prepare_page_nav()
	 *
	 * @param $where_time
	 * @param $kat_id
	 * @param string $where_content
	 * @return
	 **/
	function fun_prepare_page_nav($where_time,$kat_id,$where_content = "") {
		$where_kat = "";
		$button_kat_id = "";
		$where_erweiterung = "";
		if ($kat_id >= 1 OR $where_content != "") {
			$this->kat_rek($kat_id,"ids");
			$unter_kats = $this->kat_return_value();
			foreach ($unter_kats AS $ukat_id) {
				$where_erweiterung .= " OR ".$this->config["prefix"]."main.kat = \"$ukat_id\"";
			}
			$where_kat = " AND (".$this->config["prefix"]."main.kat = \"$kat_id\"$where_erweiterung)";
			$button_kat_id = "&kat_id=" . $kat_id;
		}
		$anzahl_news_aktuell = $this->db_anzahl_zeilen("SELECT news_id FROM ".$this->config["prefix"]."main WHERE active=\"1\" $where_time $where_kat $where_content");
		return array($anzahl_news_aktuell,$where_kat,$button_kat_id);
	}

	/**
	 * functions::fun_send_email()
	 *
	 * @param $to_email
	 * @param $from_email
	 * @param $b_lang
	 * @param $m_lang
	 * @param $search_array
	 * @return
	 **/
	function fun_send_email($to_email,$from_email,$b_lang,$m_lang,$search_array) {
		$betreff = $this->language[$b_lang];
		$message = $this->language[$m_lang];
		foreach ($search_array as $search => $replace) {
			$message = str_replace($search,$replace,$message);
		}
		mail("$to_email", $betreff, $message,"From: $from_email\nReply-To: $from_email");
		echo $message;
	}
	/**
	 * functions::fun_search()
	 *
	 * @param $search_id
	 * @param integer $minimum
	 * @param string $standard_zwischen
	 * @param array $search_array
	 * @return
	 **/
	function fun_search($search_id,$minimum = 4,$standard_zwischen = "AND",$search_array = array()) {
		$abfrage = "";
		$zwischen_muss = 0;
		if(!isset($search_array["content"])) $suche = $this->db_abfrage_az("SELECT * FROM ".$this->config["prefix"]."search WHERE id=\"$search_id\"");
		else $suche = $search_array;
		if(!isset($suche["kat_id"])) $suche["kat_id"] = 0;
		$words = explode(" ",$suche["content"]);
		if($suche["n_title"] == 1 AND $suche["n_text"] == 1) $match = "n_title,n_text";
		elseif ($suche["n_title"] == 1 AND $suche["n_text"] == 0) $match = "n_title";
		else $match = "n_text";
		foreach ($words as $wort) {
			if (strlen($wort) >= $minimum) {
				$zwischen = $this->uf_form_conf_def("",array("AND","OR","NOT"),"","valid_input",$wort);
				if ($zwischen == 0) {
					if($zwischen_muss == 1) {
						$abfrage .= " " .$standard_zwischen . " ";
						$zwischen_muss = 0;
					}
				    $abfrage .= " MATCH ($match) AGAINST (\"$wort\") ";
					$zwischen_muss = 1;
				}else {
					if($wort == "NOT") $abfrage .= "AND NOT ";
					else $abfrage .= $wort;
					$zwischen_muss = 0;
				}
			}
		}
		if($abfrage == "") $return = "";
		else $return = " AND ($abfrage) ";
		return array($return,$suche["kat_id"]);
	}
	/**
	 * functions::fun_check_speed()
	 *
	 * @author Armin
	 *
	 */
	function fun_check_speed ($check_name,$mode = 1) {
		if($mode == 0 OR $mode == 1) {
			$time = $this->fun_getmicrotime();
			if($mode == 0) {
				$this->fun_check_speed_array[$check_name] = array();
				$this->fun_check_speed_array[$check_name]["start"] = $time;
			}
			elseif($mode == 1) $this->fun_check_speed_array[$check_name]["ende"] = $time;
		}elseif($mode == 2) return $this->fun_check_speed_array[$check_name]["ende"] - $this->fun_check_speed_array[$check_name]["start"];

	}

	/**
	 * @author Armin
	 *
	 */
	 function fun_check_speed_report () {
	 	$complete_time = 0;
		foreach($this->fun_check_speed_array as $check_name => $one_array) {
			$used_time = $this->fun_check_speed($check_name,2);
			echo $check_name . ":" . $used_time . "<br />";
			$complete_time += $used_time;
		}
		return "COMPLETE: " . $complete_time;
	}

}

?>